﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Public Class MetaDataType

    Public Sub New(ByVal node As XElement, ByVal metaData As XmlMetaData)
        _node = node
        _metaData = metaData
    End Sub

    Private _node As XElement
    Private _metaData As XmlMetaData

    ''' <summary>
    ''' Gets the name that this type will use in .Net code
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MemberName() As String
        Get
            Return _node.Attribute("Name").Value
        End Get
    End Property


    ''' <summary>
    ''' Finds a column on this type by its name
    ''' </summary>
    ''' <param name="name">The name of the column to find</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Column(ByVal name As String) As MetaDataColumn

        Dim q = From c In _node.Descendants(Names.ColumnXName) _
                Where c.Attribute("Name") IsNot Nothing AndAlso c.Attribute("Name").Value.Equals(name, Strings.DefaultComparisonType) _
                Select c

        If q.Count = 0 Then
            Throw New ColumnNotFoundException(name)
        End If

        Dim col As New MetaDataColumn(q.First(), _metaData)
        Return col

    End Function

    ''' <summary>
    ''' Finds an association on this type by its name
    ''' </summary>
    ''' <param name="name">The name of the Association to find</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Association(ByVal name As String) As MetaDataAssociation

        Dim q = From c In _node.Descendants(Names.AssociationXName) _
                Where c.Attribute("Name") IsNot Nothing AndAlso c.Attribute("Name").Value.Equals(name, Strings.DefaultComparisonType) _
                Select c

        If q.Count = 0 Then
            Throw New AssociationNotFoundException(name)
        End If

        Dim col As New MetaDataAssociation(q.First(), _metaData)
        Return col

    End Function

    ''' <summary>
    ''' Renames this type.
    ''' </summary>
    ''' <param name="newName">The new name for this type.</param>
    ''' <remarks></remarks>
    Public Sub RenameTo(ByVal newName As String)

        Dim node As XElement = _node
        Dim nameNode As String = "Name"

        If node.Attribute(nameNode) IsNot Nothing Then
            node.SetAttributeValue(nameNode, newName)
        End If

    End Sub

End Class
